﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web4Comment.Service
{
    //public class CommentService:ICommentService
    public class CommentService
    {
        private static UnlessComment.BLL.Comment comment = new UnlessComment.BLL.Comment();
        private static UnlessComment.BLL.User user = new UnlessComment.BLL.User();
        private static UnlessComment.BLL.News news = new UnlessComment.BLL.News();
        private static UnlessComment.BLL.Reply reply = new UnlessComment.BLL.Reply();
        private static UnlessComment.BLL.Friend friend = new UnlessComment.BLL.Friend();
        private static UnlessComment.BLL.Recommender recommender = new UnlessComment.BLL.Recommender();

        public static int commentNews(string username, string url, string comments)
        {

            int news_id = -1;
            int user_id = -1;
                
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            List<UnlessComment.Model.News> newsList = new List<UnlessComment.Model.News>();

            userList = user.GetModelList("username = '" + username + "'");

            if (userList.Count == 1)
                user_id = userList[0].id;
            else
                return 0;

            newsList = news.GetModelList("url = '" + url + "'");
            if (newsList.Count == 0)
                return 0;
            else
                news_id = newsList[0].id;

            UnlessComment.Model.Comment commentModel = new UnlessComment.Model.Comment();
            commentModel.content = comments;
            commentModel.date = DateTime.Now;
            commentModel.news_id = news_id;
            commentModel.state = 1;
            commentModel.user_id = user_id;
            return comment.Add(commentModel);

        }
        
        public static int commentCommentary(string username, string url, int commentaryId, string comments)
        {
            int commentId = commentNews(username, url, comments);
            if ( commentId > 0)
            {
                UnlessComment.Model.Reply replyModel = new UnlessComment.Model.Reply();
                replyModel.comment_id = commentId;
                replyModel.date = DateTime.Now;
                replyModel.replied_id = commentaryId;
                return reply.Add(replyModel);
            }
            else
                return 0;
        }

        public static int recommendCommentary(int fromId, int toId, int commentaryId)
        {
            return 0;
        }

        public static List<UnlessComment.Model.CommentDetail> getNewsComments(string newsUrl)
        {
            if (newsUrl == null || newsUrl == "")
            {
                return null;
            }

            List<UnlessComment.Model.CommentDetail> list = new List<UnlessComment.Model.CommentDetail>();
            
            List<UnlessComment.Model.Comment> commentList = new List<UnlessComment.Model.Comment>();
            List<UnlessComment.Model.News> newsList = new List<UnlessComment.Model.News>();
            List<UnlessComment.Model.Reply> replyList = new List<UnlessComment.Model.Reply>();

            int new_id = -1;
            newsList = news.GetModelList("url = '" + newsUrl +"'");
            if (newsList.Count == 0)
                return null;
            else
                new_id = newsList[0].id;

            commentList = comment.GetModelList("news_id = " + new_id);

            if (commentList.Count == 0)
                return null;

            for (int i = 0; i < commentList.Count; i++)
            {
                replyList = reply.GetModelList("comment_id = " + commentList[i].id);
                if (replyList.Count == 1)
                {

                    commentList[i].content = "回复" + replyList[0].replied_id + ":" + getCommentById(replyList[0].replied_id.Value).content + ":" + commentList[i].content;
                }

                UnlessComment.Model.CommentDetail temp = new UnlessComment.Model.CommentDetail(commentList[i]);
                temp.user_username = user.GetModel((commentList[i].user_id).Value).username;
                temp.news_url = news.GetModel((commentList[i].news_id).Value).url;
                list.Add(temp);
            }

            if (list != null)
            {
                list.Sort(CompareCommentByDateIncrease);
            }

            return list;
        }

        public static List<UnlessComment.Model.CommentDetail> getMyComments(string username)
        {

            int myId; 

            List<UnlessComment.Model.CommentDetail> list = new List<UnlessComment.Model.CommentDetail>();
            List<UnlessComment.Model.Comment> commentList = new List<UnlessComment.Model.Comment>();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            List<UnlessComment.Model.Reply> replyList = new List<UnlessComment.Model.Reply>();


            userList = user.GetModelList("username = '" + username + "'");

            if (userList.Count == 1)
                myId = userList[0].id;
            else
                return list;

            //commentList = comment.GetModelListByPage("user_id = " + myId, "id", startIndex, endIndex);
            commentList = comment.GetModelList("user_id = " + myId);

            if (commentList.Count == 0)
                return list;

            for (int i = 0; i < commentList.Count; i++)
            {
                UnlessComment.Model.CommentDetail temp = new UnlessComment.Model.CommentDetail(commentList[i]);
                temp.user_username = user.GetModel((commentList[i].user_id).Value).username;
                
                temp.news_url = news.GetModel((commentList[i].news_id).Value).url;
                replyList = reply.GetModelList("comment_id = " + commentList[i].id);
                if (replyList.Count == 1)
                {
                    int replied_id = replyList[0].replied_id.Value;
                    temp.content = "回复 " + replied_id + ":" + getCommentById(replied_id).content + ":" + commentList[i].content;
                    temp.to_username = user.GetModel(comment.GetModel(replied_id).user_id.Value).username;
                }
                list.Add(temp);
            }

            if (list != null)
            {
                list.Sort(CompareCommentByDateIncrease);
            }

            return list;
        }

        public static List<UnlessComment.Model.CommentDetail> getFriendComments(string username)
        {
            int myId;

            List<UnlessComment.Model.CommentDetail> list = new List<UnlessComment.Model.CommentDetail>();

            List<UnlessComment.Model.Comment> commentList = new List<UnlessComment.Model.Comment>();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            List<UnlessComment.Model.Friend> friendList = new List<UnlessComment.Model.Friend>();
            List<UnlessComment.Model.Reply> replyList = new List<UnlessComment.Model.Reply>();


            userList = user.GetModelList("username = '" + username + "'");

            if (userList.Count == 1)
                myId = userList[0].id;
            else
                return list;

            friendList = friend.GetModelList("host_id = " + myId);
            if (friendList.Count == 0)
                return list;

            for (int j = 0; j < friendList.Count; j++)
            {

                commentList = comment.GetModelList("user_id = " + friendList[j].friend_id);

                if (commentList.Count == 0)
                {
                }
                else
                {
                    for (int i = 0; i < commentList.Count; i++)
                    {
                        UnlessComment.Model.CommentDetail temp = new UnlessComment.Model.CommentDetail(commentList[i]);
                        temp.user_username = user.GetModel((commentList[i].user_id).Value).username;
                        temp.news_url = news.GetModel((commentList[i].news_id).Value).url;
                        replyList = reply.GetModelList("comment_id = " + commentList[i].id);
                        if (replyList.Count == 1)
                        {
                            int replied_id = replyList[0].replied_id.Value;
                            temp.content = "回复 " + replied_id + ":" + getCommentById(replied_id).content + ":" + commentList[i].content;
                            temp.to_username = user.GetModel(comment.GetModel(replied_id).user_id.Value).username;
                        }
                        list.Add(temp);
                    }
                }
            }


            if (list != null)
            {
                list.Sort(CompareCommentByDate);
            }

            //list = list.GetRange(startIndex, endIndex - startIndex + 1);

            return list;
        }

        public static List<UnlessComment.Model.CommentDetail> getFriendRecommendation(string username)
        {
            int myId;

            List<UnlessComment.Model.CommentDetail> list = new List<UnlessComment.Model.CommentDetail>();

            UnlessComment.Model.News newsModel = new UnlessComment.Model.News();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            List<UnlessComment.Model.Recommender> recommenderList = new List<UnlessComment.Model.Recommender>();



            userList = user.GetModelList("username = '" + username + "'");

            if (userList.Count == 1)
                myId = userList[0].id;
            else
                return list;

            recommenderList = recommender.GetModelList("toUser_id = " + myId);
            if (recommenderList.Count == 0)
                return list;

            for (int j = 0; j < recommenderList.Count; j++)
            {

                newsModel = news.GetModel(recommenderList[j].news_id);

                if (newsModel == null)
                {
                }
                else
                {
                    UnlessComment.Model.CommentDetail temp = new UnlessComment.Model.CommentDetail();
                    temp.id = recommenderList[j].id;
                    temp.date = newsModel.date.Value; //有待商榷
                    temp.news_url = newsModel.url;
                    temp.user_username = user.GetModel(recommenderList[j].fromUser_id).username;
                    temp.to_username = username;
                    temp.content = "推荐新闻：";
                    list.Add(temp);
                }
            }


            if (list != null)
            {
                list.Sort(CompareCommentByDate);
            }

            //list = list.GetRange(startIndex, endIndex - startIndex + 1);

            return list;
        }

        public static List<UnlessComment.Model.CommentDetail> getMyRecommendation(string username)
        {
            int myId;

            List<UnlessComment.Model.CommentDetail> list = new List<UnlessComment.Model.CommentDetail>();

            UnlessComment.Model.News newsModel = new UnlessComment.Model.News();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            List<UnlessComment.Model.Recommender> recommenderList = new List<UnlessComment.Model.Recommender>();



            userList = user.GetModelList("username = '" + username + "'");

            if (userList.Count == 1)
                myId = userList[0].id;
            else
                return list;

            recommenderList = recommender.GetModelList("fromUser_id = " + myId);
            if (recommenderList.Count == 0)
                return list;

            for (int j = 0; j < recommenderList.Count; j++)
            {

                newsModel = news.GetModel(recommenderList[j].news_id);

                if (newsModel == null)
                {
                }
                else
                {
                    UnlessComment.Model.CommentDetail temp = new UnlessComment.Model.CommentDetail();
                    temp.id = recommenderList[j].id;
                    temp.date = newsModel.date.Value; //有待商榷
                    temp.news_url = newsModel.url;
                    temp.user_username = username;
                    temp.to_username = user.GetModel(recommenderList[j].toUser_id).username;
                    temp.content = "推荐了：";
                    list.Add(temp);
                }
            }


            if (list != null)
            {
                list.Sort(CompareCommentByDate);
            }

            //list = list.GetRange(startIndex, endIndex - startIndex + 1);

            return list;
        }

        public static bool deleteComment(int commentId)
        {
            return comment.Delete(commentId);
        }

        public static bool deleteRecommendation(int recommendationId)
        {
            return recommender.Delete(recommendationId);
        }

        private static int CompareCommentByDate(UnlessComment.Model.CommentDetail x, UnlessComment.Model.CommentDetail y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                if (y == null)
                {
                    return -1;
                }
                else
                {
                    if (x.date.Value == null)
                    {
                        if (y.date.Value == null)
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                    else
                    {
                        if (y == null)
                        {
                            return -1;
                        }
                        else
                        {

                            DateTime xdate = x.date.Value;
                            DateTime ydate = y.date.Value;

                            if (xdate == ydate)
                            {
                                return 0;
                            }
                            else if (xdate < ydate)
                            {
                                return 1;
                            }
                            else
                            {
                                return -1;
                            }
                        }
                    }

                }
            }
        }
        private static int CompareCommentByDateIncrease(UnlessComment.Model.CommentDetail y, UnlessComment.Model.CommentDetail x)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                if (y == null)
                {
                    return -1;
                }
                else
                {
                    if (x.date.Value == null)
                    {
                        if (y.date.Value == null)
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                    else
                    {
                        if (y == null)
                        {
                            return -1;
                        }
                        else
                        {

                            DateTime xdate = x.date.Value;
                            DateTime ydate = y.date.Value;

                            if (xdate == ydate)
                            {
                                return 0;
                            }
                            else if (xdate < ydate)
                            {
                                return 1;
                            }
                            else
                            {
                                return -1;
                            }
                        }
                    }

                }
            }
        }

        public static UnlessComment.Model.Comment getCommentById(int id)
        {
            return comment.GetModel(id);
        }
    
    }

}
